package renpyas3.view.instructions
{
	import flash.events.Event;
	import renpyas3.model.RenpyPlayerModel;
	import renpyas3.model.structs.RenpyCharacter;
	import renpyas3.view.components.Hud;
	import renpyas3.view.RenpyPlayer;
	
	/**
	 * text for a character speaking
	 */
	public class ViewInstruction_textFromChar extends ViewInstruction
	{
		public static const NAME:String = "textFromChar";

		private var _view:RenpyPlayer;
		
		public function ViewInstruction_textFromChar() 
		{
			
		}
		
		/**
		 * execute a the code for this command, altering RenpyPlayerViewState
		 * @param	view  is the whole set of view state variables and operations that make up the VIEW
		 * @param	model the data needed to play a renpy novel
		 * @param	params   extra data for some instructions
		 */
		override public function execute(view:RenpyPlayer, model:RenpyPlayerModel, params:Object=null):void
		{
			_view = view;
			
			var renpyCharacter:RenpyCharacter = params.char as RenpyCharacter;
			//view.hud.text = "<b>"+renpyCharacter.name +"</b><br>"+ String(params.text);
			//Logger.info("ViewInstruction_textFromChar::execute  --textFromChar:", params.text);

			view.hud.animateTextWriting(String(params.text),renpyCharacter.name,renpyCharacter.color);
			//view.hud.animateTextWriting("<b>"+ +"</b><br>"+ String(params.text));
			view.hud.addEventListener(Hud.FINISHED_ANIMATING_TEXT_WRITING, handleTextWritingAnimFinished, false, 0, true);
		}		

		/**
		 * finish immediately, FastForward writing anim
		 */
		override public function abort():void
		{
			_view.hud.abortTextWritingAnim();
			handleTextWritingAnimFinished();
		}
		
		private function handleTextWritingAnimFinished(e:Event=null):void 
		{
			_view.hud.removeEventListener(Hud.FINISHED_ANIMATING_TEXT_WRITING, handleTextWritingAnimFinished);

			//all done in this command
			_view.instructionsProcessor.instructionFinished();	
			
			//clean references
			_view = null;
		}
		
	}

}